package com.sharer.last.controller;


import com.sharer.last.annotation.VisitLogger;
import com.sharer.last.base.constant.CommonConstant;
import com.sharer.last.base.enums.ExceptionCode;
import com.sharer.last.base.model.PageVO;
import com.sharer.last.base.model.R;
import com.sharer.last.base.utils.StringUtil;
import com.sharer.last.enums.VisitBehavior;
import com.sharer.last.model.dto.blog.BlogIdAndTokenDTO;
import com.sharer.last.model.dto.blog.BlogPasswordDTO;
import com.sharer.last.model.dto.blog.BlogQueryDTO;
import com.sharer.last.model.dto.blog.BlogValidateDTO;
import com.sharer.last.model.vo.blog.BlogJwtVO;
import com.sharer.last.model.vo.blog.BlogVO;
import com.sharer.last.model.vo.blog.NewBlogVO;
import com.sharer.last.model.vo.blog.SearchBlogVO;
import com.sharer.last.service.BlogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 博客相关
 *
 * @ClassName BlogController
 * @Author wangjin
 * @Date 2023/5/5 21:05
 * @Description
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping(CommonConstant.BLOG_FRONT_API + "/blog")
public class BlogController {

    @Resource
    BlogService blogService;

    /**
     * 按id获取公开博客详情
     *
     * @param blogIdAndTokenDTO
     * @return
     */
    @VisitLogger(VisitBehavior.BLOG)
    @PostMapping("/getBlogById")
    public R<BlogVO> getBlogById(@RequestBody @Validated BlogIdAndTokenDTO blogIdAndTokenDTO) {
        BlogVO blog = blogService.findBlogByToken(blogIdAndTokenDTO.getId(), blogIdAndTokenDTO.getToken());
        return R.success(blog);
    }

    /**
     * 按id获取博客详情
     *
     * @param id 博客id
     * @return
     */
    @GetMapping("/findById/{id}")
    public R<NewBlogVO> findById(@PathVariable("id") Long id) {
        NewBlogVO blog = blogService.findById(id);
        return R.success(blog);
    }

    /**
     * 分页查询
     *
     * @param queryDTO
     * @return
     */
    @VisitLogger(VisitBehavior.INDEX)
    @PostMapping("/selectListByPage")
    public R<PageVO<BlogVO>> selectListByPage(@RequestBody BlogQueryDTO queryDTO) {
        PageVO<BlogVO> list = blogService.selectListByPage(queryDTO);
        return R.success(list);
    }

    /**
     * 按关键字根据文章内容搜索公开且无密码保护的博客文章
     *
     * @param queryDTO
     * @return
     */
    @VisitLogger(VisitBehavior.SEARCH)
    @PostMapping("/searchBlog")
    public R searchBlog(@RequestBody BlogQueryDTO queryDTO) {
        //校验关键字字符串合法性
        String keyword = queryDTO.getKeyword();
        if (StringUtil.isBlank(keyword)) {
            return R.error(ExceptionCode.PARAMS_ERROR);
        }
        List<SearchBlogVO> searchBlogList = blogService.selectBlogListBySearch(queryDTO);
        return R.success(searchBlogList);
    }

    /**
     * 校验受保护文章密码是否正确，正确则返回jwt
     *
     * @param dto 博客id、密码
     * @return
     */
    @VisitLogger(VisitBehavior.CHECK_PASSWORD)
    @PostMapping("/checkBlogPassword")
    public R<BlogJwtVO> checkBlogPassword(@RequestBody @Validated BlogPasswordDTO dto) {
        BlogJwtVO blogJwtVO = blogService.checkBlogPassword(dto);
        return R.success(blogJwtVO);
    }

    /**
     * 校验博客密码是否正确
     *
     * @param dto
     * @return
     */
    @PostMapping("/validateBlogPasswordCorrect")
    public R<Boolean> validateBlogPasswordCorrect(@RequestBody BlogValidateDTO dto) {
        Boolean res = blogService.validateBlogPasswordCorrect(dto);
        return R.success(res);
    }
}
